package team.seekers.hula.course.structure.section.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import team.seekers.hula.course.resource.sectionContent.service.SectionContentManageService;
import team.seekers.hula.course.structure.section.dao.SectionDao;
import team.seekers.hula.course.structure.section.entity.dao.SectionDO;
import team.seekers.hula.course.structure.section.entity.dto.SectionDTO;
import team.seekers.hula.course.structure.section.entity.dto.SectionListDTO;
import team.seekers.hula.course.structure.section.entity.vo.SectionBasicListVO;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * date: 2022/5/11 10:55
 *
 * @author GH
 * QQ 1124813753
 */
@Service
public class SectionManageService {

    @Autowired
    private SectionDao sectionDao;

    @Autowired
    private SectionContentManageService sectionContentManageService;

    @Transactional(rollbackFor = Exception.class)
    public void save(SectionDTO sectionDTO) {
        SectionDO sectionDO = SectionDO.fromDTO(sectionDTO);
        sectionDao.saveOrUpdate(sectionDO);

        List<SectionDO> list = sectionDao.lambdaQuery().select(
                SectionDO::getId,
                SectionDO::getChapterId
        ).eq(SectionDO::getChapterId, sectionDTO.getChapterId()).orderByAsc(SectionDO::getGmtCreate).list();

        ArrayList<String> sections = new ArrayList<>();
        for (SectionDO aDo : list) {
            sections.add(aDo.getId());
        }
        sections.add(sectionDO.getId());
    }

    public void update(SectionDTO sectionDTO) {
        sectionDao.saveOrUpdate(SectionDO.fromDTO(sectionDTO));
    }

    public List<SectionListDTO> queryDetailSectionsByChapterId(String chapterId) {
        List<SectionDTO> sections = SectionDO.toDTOList(sectionDao.lambdaQuery().select(
                SectionDO::getId,
                SectionDO::getName
        ).eq(SectionDO::getChapterId, chapterId).list());

        return sections.stream().map(sectionDTO ->
                SectionListDTO.builder()
                        .id(sectionDTO.getId())
                        .name(sectionDTO.getName())
                        .contents(sectionContentManageService.querySectionContentsBySectionId(sectionDTO.getId()))
                        .build()).collect(Collectors.toList());
    }

    public List<String> getSectionIdsByCourseId(Long courseId) {
        List<SectionDO> list = sectionDao.lambdaQuery().select(
                SectionDO::getId
        ).eq(SectionDO::getCourseId, courseId).list();

        ArrayList<String> sectionIds = new ArrayList<>();
        for (SectionDO sectionDO : list) {
            sectionIds.add(sectionDO.getId());
        }
        return sectionIds;
    }

    public List<SectionBasicListVO> getBasicVOListByCourseId(String courseId) {
        List<SectionDO> list = sectionDao.lambdaQuery().select(
                SectionDO::getId,
                SectionDO::getName
        ).eq(SectionDO::getCourseId, courseId).list();

        ArrayList<SectionBasicListVO> sectionBasicListVOS = new ArrayList<>();
        for (SectionDO sectionDO : list) {
            SectionBasicListVO sectionBasicListVO = SectionBasicListVO.builder().id(sectionDO.getId()).name(sectionDO.getName()).build();
            sectionBasicListVOS.add(sectionBasicListVO);
        }
        return sectionBasicListVOS;
    }

//    @Transactional(rollbackFor = Exception.class)
//    public void delete(Long sectionId, ChapterManageDTO chapterManageDTO) {
//        sectionDao.removeById(sectionId);
//        chapterManageService.update(chapterManageDTO);
//    }
}
